package com.scalar.forecast.tags;

import com.scalar.forecast.columnDisplay.ColumnMetaData;
import com.scalar.forecast.projectSetup.Project;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.commons.Constants;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.Labels;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import org.apache.struts.util.MessageResources;

/**
 * Tag for displaying search results
 *
 * Created by Veeru. 
 * Date: Feb 17, 2005
 * Time: 12:45:05 PM 
 */

public class ProjectSearchResultsTag extends TagSupport {


    public int doEndTag() throws JspException {

        HttpSession    session = pageContext.getSession();
        JspWriter      out     = pageContext.getOut();
        StringBuffer   result  = new StringBuffer();

        int pageNumber     = 1;
        int recordsPerPage = 0;
        int startIndex = 0;
        int endIndex   = 0;
        String chipTracContext = "/" + ConfigManager.getChipTracContext();

        try {
            pageNumber = Integer.parseInt( ( ( String )session.getAttribute( SessionKeys.REQUEST_PAGE_NUMBER ) ).trim() );
        } catch ( Exception e ){
            pageNumber = 0;
        }

        recordsPerPage = Constants.SERACH_RESULTS_PER_SCREEN;

        ArrayList columnMetaDataList = (ArrayList)session.getAttribute("columnMetaDataList");

        result.append("<TR>");
        for ( int i = 0; i < columnMetaDataList.size() ; i++ ) {
            ColumnMetaData columnMetaData= (ColumnMetaData)columnMetaDataList.get( i ) ;
            result.append("<TD align=middle width='18%' bgColor='#993333'><FONT class=NArialL> <FONT color='#ffffff'>");
            result.append( columnMetaData.getDisplayName() );
            result.append("</FONT></FONT></TD>");
        }
        result.append("</TR>");
        result.append("");

        ArrayList projects = ( ArrayList )session.getAttribute( "projects" );

        startIndex = ( pageNumber - 1 ) * recordsPerPage ;
        endIndex   = pageNumber * recordsPerPage;

        if ( endIndex > projects.size() ) {
            endIndex = projects.size() ;
        }
        User user = ( User )session.getAttribute( SessionKeys.USER_KEY );
        if ( user != null ) {
            boolean showModify = user.isActionAllowed( Functionality.PROJECT_SETUP, User.USER_ACTION_MODIFY );
            boolean showDelete = user.isActionAllowed( Functionality.PROJECT_SETUP, User.USER_ACTION_DELETE );
            for (int i = startIndex; i < endIndex; i++) {
                Project project = ( Project ) projects.get( i );
                result.append( "<TR bgcolor='#FFFFEE'>" );
                String endCustomerLabel = null;
                try {
                    endCustomerLabel = MessageResources.getMessageResources(Labels.APPLICATION_RESOURCE_PROPERTIES).getMessage("label.sales.endCustomer");
                    if (endCustomerLabel == null) {
                        endCustomerLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                    }
                }
                catch (NullPointerException e) {
                    endCustomerLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                }
                String endUserLabel = null;
                try {
                    endUserLabel = MessageResources.getMessageResources(Labels.APPLICATION_RESOURCE_PROPERTIES).getMessage("label.sales.endUser");
                    if (endUserLabel == null) {
                        endUserLabel = Labels.DEFAULT_END_USER_LABEL;
                    }
                }
                catch (NullPointerException e) {
                    endUserLabel = Labels.DEFAULT_END_USER_LABEL;
                }
                for ( int j = 0; j < columnMetaDataList.size() ; j++ ) {
                    ColumnMetaData columnMetaData= ( ColumnMetaData )columnMetaDataList.get( j ) ;
                    if ( "Project-Part Relationships".equalsIgnoreCase(columnMetaData.getColumnName()) ){
                        result.append( "<td align=left width='28%'");
                    } else {
                        result.append( "<td align=center width='18%' valign='top'");
                    }
                    result.append( ( i % 2 == 0 ?  "bgcolor='#FFFFEE'" : "bgcolor='#DEE1E2'" ) + "><FONT class=NArialL>");

                    if ( "Project Name".equalsIgnoreCase( columnMetaData.getColumnName() ) ) {
                        result.append( project.getProjectName() );
                    }
                    else if ( "Project-Part Relationships".equalsIgnoreCase(columnMetaData.getColumnName()) ) {
                        result.append( "&nbsp;&nbsp;&nbsp;&nbsp;"+project.getPartsList().replaceAll(",","<br>&nbsp;&nbsp;&nbsp;&nbsp;") );
                    } else if ( "Customer Name".equalsIgnoreCase( columnMetaData.getColumnName() ) ) {
                        result.append( project.getCustomerName() );
                    } else if ( "Actions".equalsIgnoreCase( columnMetaData.getColumnName() ) ) {
                        result.append("<a href="+chipTracContext+"/projectSetup.do?action=view&projectID="+project.getProjectID()+" onmouseover=\"window.status='Click here to view Project Setup';return true\" onmouseout=\"window.status='';return true\" title='Click here to view Project Setup'>View</a>&nbsp;&nbsp;&nbsp;&nbsp;");
                        if( showModify ) {
                            result.append("<a href="+chipTracContext+"/projectSetup.do?action=modify&projectID="+project.getProjectID()+" onmouseover=\"window.status='Click here to modify Project Setup';return true\"  onmouseout=\"window.status='';return true\" title='Click here to modify Project Setup'>Modify</a>&nbsp;&nbsp;&nbsp;&nbsp;");
                        }
                        if( showDelete ){
                            result.append("<a href="+chipTracContext+"/projectSetup.do?action=view&purpose=delete&projectID="+project.getProjectID()+" onmouseover=\"window.status='Click here to delete Project Setup';return true\" onmouseout=\"window.status='';return true\" title='Click here to delete Project Setup'>Delete</a>");
                        }
                    }else if( endCustomerLabel.equalsIgnoreCase( columnMetaData.getColumnName() ) ){
                        result.append( project.getOdm() );

                    }else if( endUserLabel.equalsIgnoreCase( columnMetaData.getColumnName() ) ){
                        result.append( project.getEndUserName() );

                    }else if ( "Sales Person".equalsIgnoreCase( columnMetaData.getColumnName() ) ) {
                        result.append( project.getSalesPerson() );
                    }
                    result.append("</td>");
                }
                result.append("</TR>");
            }
            try {
                out.write( result.toString() );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
        return ( EVAL_PAGE );
    }
}
